//
//  TRVerifyPhoneViewController.m
//  WeiMiLan
//
//  Created by anan on 14/12/9.
//  Copyright (c) 2014年 Mac. All rights reserved.
//

#import "TRVerifyPhoneViewController.h"

@interface TRVerifyPhoneViewController ()

@end

@implementation TRVerifyPhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.verifCode = @"88888";
    self.titleNum = 60;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)sendMsg
{
    
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS
     //这个参数可以选择是通过发送验证码还是语言来获取验证码
                            phoneNumber:self.textFieldPhoneNum.text
                                   zone:@"86"
                       customIdentifier:nil //自定义短信模板标识
                                 result:^(NSError *error)
     {
         [QLMaskUtil hideLoadingMaskWithView:self.view];
         if (!error)
         {
             [SVProgressHUD showImage:nil status:@"验证码已发送"];
             NSLog(@"block 获取验证码成功");
             
         }
         else
         {
             
             [SVProgressHUD showImage:nil status:@"验证码发送失败"];
         }
         
     }];
}

- (IBAction)buttonSendAction:(UIButton *)sender {
    if (self.titleNum<60) {
        return;
    }
    
    if ([self.textFieldPhoneNum.text isEqualToString:@""]) {
        [SVProgressHUD showImage:nil status:@"请输入手机号"];
        return;
    }
    if (![self isMobileNumber:self.textFieldPhoneNum.text]) {
        [SVProgressHUD showImage:nil status:@"手机号格式有误"];
        return;
    }
//    __weak typeof(self) weakSelf = self;
//    [ApplicationDelegate.webAPI getWebAPIWithPath:@"http://ping.vsmu.cn" andParams:@{@"phone":self.textFieldPhoneNum.text,@"mac":@""} andMethodNode:@"app/verification-code" completed:^(NSInteger state, AFHTTPRequestOperation *operation, id responseObject) {
//        if (state==1) {
//            if ([responseObject[@"code"] intValue]==0) {
//                dispatch_async(dispatch_get_main_queue(), ^{
//                    weakSelf.verifCode = responseObject[@"verifCode"];
//                    [SVProgressHUD showImage:nil status:@"验证码已发送"];
//                });
//            }
//        }
//    }];

    [self sendMsg];
    
    sender.backgroundColor = [UIColor lightGrayColor];
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeBtnTitle:) userInfo:nil repeats:YES];
    
}

-(void)changeBtnTitle:(NSTimer *)timer{
    
    [self.buttonSend setTitle:[NSString stringWithFormat:@"%d秒重获取",self.titleNum--] forState:UIControlStateNormal];
    if (self.titleNum==0) {
        [timer invalidate];
        self.titleNum = 60;
        self.buttonSend.backgroundColor = [UIColor purpleColor];
        [self.buttonSend setTitle:@"获取验证码" forState:UIControlStateNormal];
    }
}

- (IBAction)buttonBackAction:(UIButton *)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)buttonNextAction:(UIButton *)sender {
    
    [QLMaskUtil showMaskLoadingWithView:self.view];
    
    [SMSSDK  commitVerificationCode:self.textFieldVerifyCode.text
     //传获取到的区号
                        phoneNumber:self.textFieldPhoneNum.text
                               zone:@"86"
                             result:^(NSError *error)
     {
         [QLMaskUtil hideLoadingMaskWithView:self.view];
         if (!error)
         {
             [SVProgressHUD showSuccessWithStatus:@"验证码正确"];
             [QLPropertyUtils putStringByKey:@"newPhoneNum" value:self.textFieldPhoneNum.text];
             [self buttonBackAction:sender];
             
         }
         else
         {
             
             [SVProgressHUD showImage:nil status:@"验证码错误"];
             return;
         }
         
     }];
//    if ([self.verifCode isEqualToString:self.textFieldVerifyCode.text]) {
//        [SVProgressHUD showSuccessWithStatus:@"验证码正确"];
//        [QLPropertyUtils putStringByKey:@"newPhoneNum" value:self.textFieldPhoneNum.text];
//        [self buttonBackAction:sender];
//    }else{
//        [SVProgressHUD showErrorWithStatus:@"验证码错误"];
//    }
}

//手机号码检测
- (BOOL)isMobileNumber:(NSString *)mobileNum
{
    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:mobileNum];
}

@end
